package com.yufei.utils;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;




public class PatternUtils {
	public static Log mLog = LogFactory.getLog(PatternUtils.class);



	/**用指定的一系列正则表达式对内容进行匹配，最总只返回一个匹配结果字符串并且是第一个匹配到的字符串如果匹配多个但是你想要的不是第一个那就用getStrByRegex(String content, String regexs,
			int position)
	 * @param content:要进行匹配的目标内容
	 * @param regexs：正则表达式，多个正则表达式之间以!号隔开
	 * @param isCleanHtml：是否对网页源码进行特殊的处理还是保留源码原样
	 * @return
	 */
	public static String getStrByRegex(String content, String regexs,
			boolean isCleanHtml) {
		String str = null;
		List<Pattern> patters = null;

		if (regexs == null) {
			return null;
		}

		Pattern pattern = null;
		String[] temps = null;
		List<Pattern> patterns = null;
		patterns = new ArrayList();
		temps = regexs.split("!");
		for (String reg : temps) {
			pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
			patterns.add(pattern);
		}
		str = PatternUtils.getStrByPattern(patterns, content);
		if (isCleanHtml) {
			str = CommonUtil.cleanHtml(str);
		}
		return str;

	}

	public static String getStrByRegex(String content, String regexs,
			int position) {
		String str = null;
		List<Pattern> patters = null;

		if (regexs == null) {
			return null;
		}

		Pattern pattern = null;
		String[] temps = null;
		List<Pattern> patterns = null;
		patterns = new ArrayList();
		temps = regexs.split("!");
		for (String reg : temps) {
			pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
			patterns.add(pattern);
		}
		str = PatternUtils.getStrByPattern(patterns, content, position);
		return str;

	}

	public static List<String> getListStrByRegex(String content, String regexs) {
		List<String> strs = null;
		List<Pattern> patters = null;

		if (regexs == null) {
			return null;
		}

		Pattern pattern = null;
		String[] temps = null;
		List<Pattern> patterns = null;
		patterns = new ArrayList();
		temps = regexs.split("!");
		for (String reg : temps) {
			pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
			patterns.add(pattern);
		}
		strs = PatternUtils.getListStrByPattern(patterns, content);
		return strs;

	}

	public static String getStrByRegex(Map<String, String> regexMap,
			String content, String property) {
		String str = null;
		List<Pattern> patters = null;
		String regexs = regexMap.get(property);
		if (regexs == null) {
			return null;
		}
		// 最用户配置的规则进行解析
		if (regexs.contains(";")) {
			String[] rules = regexs.split(";");
			content = PatternUtils.getStrByRegex(content, rules[0], false);
			str = PatternUtils.getStrByRegex(content, rules[1],
					(int) Integer.valueOf(rules[2]));

			return str;
		}
		str=PatternUtils.getStrByRegex(content, regexs, true);
		return str;

	}
	/**
	 * @param content
	 * @param regex TODO
	 * @return
	 * 此方法主要适用于从网页内容中根据特定的正则表达式来匹配内容
	 * 正则表达式可以有各种方式，不同的方式会对应不同的匹配方式
	 * 目前支持一下几种方式：
	 * 1:regex!regex     多个正则表达式去匹配内容，只要有一个能匹配到后面的正则表达式就会直接忽略，如果匹配多个，则取第一个匹配到的内容
	 * 2:regex~regex~position     第一个正则表达式去获取内容，然后在第一个表达式获取内容的基础上用第二个表达式去匹配，如果匹配多个，取位置为position那个
	 * 3：regex~position   第一个正则表达式去获取内容，如果匹配多个则回去第二个参数指定位置匹配到的内容
	 */
	public static String getStrByRegex(String content,
			String regexs) {
		String str = null;
		List<Pattern> patters = null;
		if (regexs == null) {
			return null;
		}
		// 最用户配置的规则进行解析
		if (regexs.contains("~")) {
			String[] rules = regexs.split("~");
			if(rules.length==3){
				content = PatternUtils.getStrByRegex(content, rules[0], false);
				str = PatternUtils.getStrByRegex(content, rules[1],
						(int) Integer.valueOf(rules[2]));	
			}
			if(rules.length==2){
				str = PatternUtils.getStrByRegex(content, rules[0], (int) Integer.valueOf(rules[1]));
				}

			return str;
		}
		str=PatternUtils.getStrByRegex(content, regexs, true);
		return str;

	}
	/**
	 * @param patterns
	 *            (里面只有一组)
	 * @param content
	 * @return 根据正则表达式和要匹配的内容匹配出字符串
	 */
	public static String getStrByPattern(List<Pattern> patterns, String content) {

		String str = null;
		if (patterns == null || patterns.size() == 0) {
			mLog.debug("正则表达式没有配置,将匹配结果设置为为空字符串！");

			return "";
		}

		// pattern=Pattern.compile("精彩15优惠套餐([\r\n|\\d|\\w|\\D]{0,}(\r\n){0,})下一条",
		// Pattern.CASE_INSENSITIVE);
		int conti = 0;
		for (Pattern pattern : patterns) {
			if (conti == 1) {
				break;
			}
			Matcher matcher = pattern.matcher(content);
			// 假定正则表达式至少有一组
			int count = 0;

			while (matcher.find()) {
				count = matcher.groupCount();
				if (count == 0) {
					str = matcher.group(0);
					conti = 1;

					break;
				} else {
					str = matcher.group(1);
					conti = 1;

					break;

				}
			}
			if (str == null || str.equals("")) {
				str = "";
				mLog.debug("正则表达式匹配失败，正则表达式类型为：" + pattern.pattern()
						+ "将匹配结果设置为为空字符串！");

				// mLog.debug("匹配失败的内容是："+content);
				mLog.debug("*********************************************************************************");

			}
		}
		return str;
	}

	/**
	 * @param patterns
	 * @param content
	 * @param position
	 *            :指定位置的字符串
	 * @return
	 */
	public static String getStrByPattern(List<Pattern> patterns,
			String content, int position) {

		String str = null;
		if (patterns == null || patterns.size() == 0) {
			mLog.debug("正则表达式没有配置,将匹配结果设置为为空字符串！");

			return "";
		}

		int conti = 0;
		for (Pattern pattern : patterns) {
			if (conti == 1) {
				break;
			}
			Matcher matcher = pattern.matcher(content);
			// 假定正则表达式至少有一组
			int count = 0;
			int po = 1;
			// 匹配到的字符串数组循环
			while (matcher.find()) {
				count = matcher.groupCount();
				if (count == 0) {
					str = matcher.group(0);
				} else {
					if (po == position) {
						str = matcher.group(1);

					}

				}
				po += 1;
				conti = 1;
			}
			if (str == null || str.equals("")) {
				str = "";
				mLog.debug("正则表达式匹配失败，正则表达式类型为：" + pattern.pattern()
						+ "将匹配结果设置为为空字符串！");

				// mLog.debug("匹配失败的内容是："+content);
				mLog.debug("*********************************************************************************");

			}
		}
		return str;
	}

	public static List<String> getListStrByPattern(List<Pattern> patterns,
			String content) {
		List<String> strs = new ArrayList();
		;
		String str;
		if (patterns == null || patterns.size() == 0) {
			mLog.debug("正则表达式没有配置,将匹配结果设置为为空字符串！");

			return strs;
		}
		int conti = 0;
		for (Pattern pattern : patterns) {
			if (conti == 1) {
				break;
			}
			Matcher matcher = pattern.matcher(content);
			// 假定正则表达式至少有一组
			int count = 0;
			String matchContent = null;
			while (matcher.find()) {
				count = matcher.groupCount();

				for (int i = 0; i < count; i++) {
					matchContent = matcher.group(i + 1);
					addUniqueElement(strs, matchContent);
					// strs.add();

				}
				if (count == 0) {
					matchContent = matcher.group(0);

					addUniqueElement(strs, matchContent);
				}

				conti = 1;
			}

		}
		if (strs.size() == 0) {
			for (Pattern pattern : patterns) {
				mLog.debug("正则表达式匹配失败，正则表达式类型为：" + pattern.pattern());
			}
			// mLog.debug("匹配失败的内容是："+content);
			mLog.debug("*********************************************************************************");

		}
		return strs;
	}
public static boolean match(String content,String regexes){
	if (CommonUtil.isEmptyOrNull(regexes)) {
		return true;
	}


  boolean isMatch=true;
  String str=PatternUtils.getStrByRegex(content, regexes);
  if(CommonUtil.isEmptyOrNull(str)){
	  isMatch=false;
  }
  return isMatch;
}
	public static void addUniqueElement(List list, Object obj) {
		if (!list.contains(obj)) {
			list.add(obj);

		}

	}


public static void main(String[] args){
	String url="http://www.nba98.com/nbalx/65265.html";
	System.out.print(PatternUtils.match(url, ".*nba1298.*!.*nbas.*"));
}



}
